//
//  ZYWebpDecoder.m
//  ZYWebImage
//
//  Created by wangzhipan on 2025/3/6.
//

#import "ZYWebpDecoder.h"
#import "demux.h"
#import "mux.h"
#import "sharpyuv.h"
#import "decode.h"
#import "encode.h"
#import "types.h"
#import "mux_types.h"
#import "format_constants.h"
#import "UIImage+Extra.h"
#import "IZYImageCacheProtocol.h"
@interface ZYWebpDecoder (){
    NSData *_data;
}

@end

@implementation ZYWebpDecoder
- (instancetype)initWithData:(NSData *)data
{
    if (self = [super init]) {
        _data = data;
    }
    return self;
}

- (UIImage *)decodeImage
{
    NSData *data = _data;
    if (!data || data.length == 0) {
        return nil;
    }
    CGFloat scale = MAX(1, [UIScreen mainScreen].scale);
    WebPData webp = { data.bytes, data.length };
        WebPDecoderConfig config;
        if (!WebPInitDecoderConfig(&config)) {
            return nil;
        }
        
        // 获取图片信息
        if (WebPGetFeatures(webp.bytes, webp.size, &config.input) != VP8_STATUS_OK) {
            return nil;
        }
        
        // 设置输出参数
        int originalWidth = config.input.width;
        int originalHeight = config.input.height;
        int scaledWidth = originalWidth * scale;
        int scaledHeight = originalHeight * scale;
        
        config.options.use_threads = 1;
        config.options.scaled_width = scaledWidth;
        config.options.scaled_height = scaledHeight;
        config.output.colorspace = MODE_RGBA;
        config.output.is_external_memory = 0;
        config.output.u.RGBA.stride = scaledWidth * 4;
        config.output.u.RGBA.size = scaledWidth * scaledHeight * 4;
        config.output.u.RGBA.rgba = (uint8_t*)malloc(config.output.u.RGBA.size);
        
        // 解码
        if (WebPDecode(webp.bytes, webp.size, &config) != VP8_STATUS_OK) {
            free(config.output.u.RGBA.rgba);
            NSLog(@"%s  decode error: WebPDecode error", __func__);
            return nil;
        }
        
        // 转换为位图
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        CGContextRef context = CGBitmapContextCreate(config.output.u.RGBA.rgba,
                                                   scaledWidth,
                                                   scaledHeight,
                                                   8,
                                                   scaledWidth * 4,
                                                   colorSpace,
                                                   kCGImageAlphaPremultipliedLast);
        
        if (!context) {
            free(config.output.u.RGBA.rgba);
            CGColorSpaceRelease(colorSpace);
            NSLog(@"%s  decode error: context is null", __func__);
            return nil;
        }
        
        CGImageRef cgImage = CGBitmapContextCreateImage(context);
        UIImage *finalImage = [UIImage imageWithCGImage:cgImage scale:scale orientation:UIImageOrientationUp];
        
        // 清理资源
        CGImageRelease(cgImage);
        CGContextRelease(context);
        CGColorSpaceRelease(colorSpace);
        free(config.output.u.RGBA.rgba);
        
        finalImage.zy_format = ZYImageFormatWebp;
        return finalImage;
}
@end
